package My.example;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

@WebServlet("/Aser1")
public class Aser1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String as="张三";
        as= URLEncoder.encode(as, "UTF-8");
        Cookie cookie=new Cookie("name",as);
        resp.addCookie(cookie);

        Cookie[] cookie1=req.getCookies();
        for (Cookie cookie2 : cookie1) {
            String name=cookie2.getName();
            String value=cookie2.getValue();
            System.out.println(name);
            value= URLDecoder.decode(value,"UTF-8");
            System.out.println(value);
            System.out.println("-------------");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
